import fetch from 'node-fetch';

const API_URL = 'http://localhost:3000/api/recognize';

async function testURLRecognition() {
  console.log('🧪 测试 URL 识别功能\n');

  // 测试用例
  const testCases = [
    {
      name: '测试 1: 使用你提供的 URL',
      url: 'http://fire.sszhai.com/5.jpg'
    },
    {
      name: '测试 2: GitHub 二维码（示例）',
      url: 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=https://github.com'
    }
  ];

  for (const testCase of testCases) {
    console.log(`\n📝 ${testCase.name}`);
    console.log(`   URL: ${testCase.url}`);
    
    try {
      const response = await fetch(API_URL, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          url: testCase.url
        })
      });

      const result = await response.json();
      
      if (result.success) {
        console.log(`   ✅ 识别成功!`);
        console.log(`   📊 识别到 ${result.count} 个二维码`);
        result.data.forEach((qr, index) => {
          console.log(`   ${index + 1}. 内容: ${qr.text}`);
        });
      } else {
        console.log(`   ❌ 识别失败: ${result.error}`);
      }
    } catch (error) {
      console.log(`   ❌ 请求失败: ${error.message}`);
    }
  }

  console.log('\n\n📚 使用说明:');
  console.log('═'.repeat(60));
  console.log('\n方法 1: 使用 JSON 格式（推荐）');
  console.log('```bash');
  console.log('curl -X POST http://localhost:3000/api/recognize \\');
  console.log('  -H "Content-Type: application/json" \\');
  console.log('  -d \'{"url":"http://fire.sszhai.com/5.jpg"}\'');
  console.log('```');
  
  console.log('\n方法 2: 使用 form-data 格式');
  console.log('```bash');
  console.log('curl -X POST http://localhost:3000/api/recognize \\');
  console.log('  -F "url=http://fire.sszhai.com/5.jpg"');
  console.log('```');

  console.log('\n方法 3: 使用 Postman');
  console.log('1. 方法: POST');
  console.log('2. URL: http://localhost:3000/api/recognize');
  console.log('3. Body → raw → JSON');
  console.log('4. 输入: {"url":"http://fire.sszhai.com/5.jpg"}');
  console.log('5. 点击 Send');

  console.log('\n方法 4: 使用 Node.js');
  console.log('```javascript');
  console.log('const response = await fetch("http://localhost:3000/api/recognize", {');
  console.log('  method: "POST",');
  console.log('  headers: { "Content-Type": "application/json" },');
  console.log('  body: JSON.stringify({ url: "http://fire.sszhai.com/5.jpg" })');
  console.log('});');
  console.log('const result = await response.json();');
  console.log('console.log(result.data[0].text);');
  console.log('```');

  console.log('\n方法 5: 使用 Python');
  console.log('```python');
  console.log('import requests');
  console.log('response = requests.post(');
  console.log('    "http://localhost:3000/api/recognize",');
  console.log('    json={"url": "http://fire.sszhai.com/5.jpg"}');
  console.log(')');
  console.log('result = response.json()');
  console.log('print(result["data"][0]["text"])');
  console.log('```');

  console.log('\n' + '═'.repeat(60));
}

// 运行测试
testURLRecognition().catch(console.error);

